<?php

/*

    MMetaverse - Open Metaverse Web Manager

    Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique

    http://www.maryjanesoft.com
    http://www.marlonj.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    Boston, MA  02111-1307  USA

    $Id$

*/

Yii::import('application.models.mmetaverse.MMetaverseModel');
Yii::import('application.models.mmetaverse.MConfigFormModel');

/**
 * Avatar controller
 */
class AdminController extends CController {

    /**
     * Returns the filter configurations.
     *
     * @return array a list of filter configurations.
     */
    public function filters()
    {
        // Enable access control
        return array(
            'accessControl',
        );
    }

    /**
     * Set Access Rules
     */
    public function accessRules() {
        return array
        (
            // Allow access to admin
            array('allow','actions' => array('index'),
                    'users' => array('admin')),
            // Deny access to everybody
            array('deny','actions' => array('index'),
                    'users' => array('*')),
        );
    }

    /**
     * Show list of admin actions 
     */
    public function actionIndex() {
        $this->render('MAdminIndexView');
    }

    /**
     * Show Admin Login
     */
    public function actionLogin() {
        // If the user is admin, show index
        if(Yii::app()->user->getName() == 'admin') {
            $this->redirect(array('index'));
        }

        // Render admin login page 
        $this->render('MAdminLoginView');
    }

    /**
     * Edit Configuration
     */
    public function actionEditConfig() {
        // Create Form
        $configForm = new MConfigFormModel();

        // Check if submit data
        if(isset($_POST['MConfigFormModel'])) {
            // Load Attributes
            $configForm->attributes = $_POST['MConfigFormModel'];

            // Validate the information
            if($configForm->validate())
            {
                // Get user manager
                $metaverse = MMetaverseModel::getMetaverse();

                // Get Current Config 
                $config = $metaverse->getConfig();

                // Update config information
                $config->setOpenSimPath($configForm->opensimPath);

                // Try to save
                try {
                    $config->setDatabaseEngine($configForm->databaseEngine);
                    $config->save();

                    // Go to the administration page
                    $this->redirect(array('index'));
                } catch (Exception $e) {
                    $configForm->addError('opensimPath',$e->getMessage());
                }                               
            }
        }
        else {
            // Get Metaverse
            $metaverse = MMetaverseModel::getMetaverse();

            // Get Configuration
            $config = $metaverse->getConfig();

            // Set Values 
            $configForm->opensimPath = $config->getOpenSimPath();
            $configForm->databaseEngine = $config->getDatabaseEngine();
        }

        // Render the form 
        $this->render('MConfigFormView',array('configForm'=>$configForm));
    }
}

?>
